package com.ajocer.springbootinit.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ajocer.springbootinit.common.ErrorCode;
import com.ajocer.springbootinit.constant.CommonConstant;
import com.ajocer.springbootinit.exception.ThrowUtils;
import com.ajocer.springbootinit.mapper.SpotFeeMapper;
import com.ajocer.springbootinit.model.dto.spotFee.SpotFeeQueryRequest;
import com.ajocer.springbootinit.model.entity.SpotFee;
import com.ajocer.springbootinit.model.entity.User;
import com.ajocer.springbootinit.model.vo.SpotFeeVO;
import com.ajocer.springbootinit.model.vo.UserVO;
import com.ajocer.springbootinit.service.SpotFeeService;
import com.ajocer.springbootinit.service.UserService;
import com.ajocer.springbootinit.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 景点门票表服务实现
 * */
@Service
@Slf4j
public class SpotFeeServiceImpl extends ServiceImpl<SpotFeeMapper, SpotFee> implements SpotFeeService {

    @Resource
    private UserService userService;

    /**
     * 校验数据
     *
     * @param spotFee
     * @param add      对创建的数据进行校验
     */
    @Override
    public void validSpotFee(SpotFee spotFee, boolean add) {
        ThrowUtils.throwIf(spotFee == null, ErrorCode.PARAMS_ERROR);
        // 创建数据时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(spotFee.getSpotId() == null || spotFee.getSpotId() <= 0, ErrorCode.PARAMS_ERROR);
        }
    }

    /**
     * 获取查询条件
     *
     * @param spotFeeQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<SpotFee> getQueryWrapper(SpotFeeQueryRequest spotFeeQueryRequest) {
        QueryWrapper<SpotFee> queryWrapper = new QueryWrapper<>();
        if (spotFeeQueryRequest == null) {
            return queryWrapper;
        }
        
        Long id = spotFeeQueryRequest.getId();
        Long spotId = spotFeeQueryRequest.getSpotId();
        Long adminId = spotFeeQueryRequest.getAdminId();
        String spotFeeName = spotFeeQueryRequest.getSpotFeeName();
        String spotFeeDescription = spotFeeQueryRequest.getSpotFeeDescription();
        Integer spotFeeStatus = spotFeeQueryRequest.getSpotFeeStatus();
        String sortField = spotFeeQueryRequest.getSortField();
        String sortOrder = spotFeeQueryRequest.getSortOrder();
        
        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(spotFeeName), "spotFeeName", spotFeeName);
        queryWrapper.like(StringUtils.isNotBlank(spotFeeDescription), "spotFeeDescription", spotFeeDescription);
        
        // 精确查询
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(spotId), "spotId", spotId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(adminId), "adminId", adminId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(spotFeeStatus), "spotFeeStatus", spotFeeStatus);
        
        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 获取景点门票表封装
     *
     * @param spotFee
     * @param request
     * @return
     */
    @Override
    public SpotFeeVO getSpotFeeVO(SpotFee spotFee, HttpServletRequest request) {
        // 对象转封装类
        SpotFeeVO spotFeeVO = SpotFeeVO.objToVo(spotFee);
        return spotFeeVO;
    }

    /**
     * 分页获取景点门票表封装
     *
     * @param spotFeePage
     * @param request
     * @return
     */
    @Override
    public Page<SpotFeeVO> getSpotFeeVOPage(Page<SpotFee> spotFeePage, HttpServletRequest request) {
        List<SpotFee> spotFeeList = spotFeePage.getRecords();
        Page<SpotFeeVO> spotFeeVOPage = new Page<>(spotFeePage.getCurrent(), spotFeePage.getSize(), spotFeePage.getTotal());
        if (CollUtil.isEmpty(spotFeeList)) {
            return spotFeeVOPage;
        }
        // 对象列表 => 封装对象列表
        List<SpotFeeVO> spotFeeVOList = spotFeeList.stream()
                .map(SpotFeeVO::objToVo)
                .collect(Collectors.toList());

        spotFeeVOPage.setRecords(spotFeeVOList);
        return spotFeeVOPage;
    }

    /**
     * 根据 ID 查询门票信息并加锁
     *
     * @param id 门票 ID
     * @return 门票信息
     */
    @Override
    public SpotFee getByIdWithLock(Long id) {
        // 使用乐观锁或悲观锁查询门票信息
        // 这里简单实现，实际应该根据具体需求选择合适的锁机制
        return this.getById(id);
    }
} 